package keyboard.service;

import keyboard.dao.NotesMapper;
import keyboard.dao.UserMapper;
import keyboard.entity.Notes;
import keyboard.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 2B键盘
 * @version 1.0
 * create: 2018-05-04 0:10
 * @desc
 **/
@Service
@Transactional
public class NotesService {

    @Autowired
    NotesMapper notesMapper;

    @Autowired
    UserService userServicer;

    /**
     *  查询用户的所有笔记
     * identity = 查找用户id
     *
     * @param identity
     * @return
     */
    public List<Notes> QueryNotes(String identity){
        int id = userServicer.getUserId(identity);
        Notes notes = new Notes();
        notes.setAuthorId(id);
        return notesMapper.selectByAuthorKey(notes);
    }

    /**
     * 修改笔记内容
     *
     * @param note
     * @param title
     * @param identity
     * @return
     */
    public int updateNotes(String note,String title,String identity){
        int id = userServicer.getUserId(identity);
        Notes notes = new Notes();
        notes.setAuthorId(id);
        notes.setTitle(title);
        notes.setContent(note);
        if(notesMapper.QuByTitle(notes) == null){
            notes.setCrateTime(new Date());
            notes.setUpdateTime(new Date());
            notesMapper.insert(notes);
            return 2;
        }
        notes.setUpdateTime(new Date());
        return notesMapper.updateByAidAndTit(notes);
    }

    /**
     * 查询单篇笔记 通过id查询
     * @param id
     * @return
     */
    public Notes QueryNotesById(Integer id){
         return notesMapper.selectByPrimaryKey(id);
    }

    /**
     * 删除笔记
     * @param id
     * @return
     */
    public int deleteNotes(Integer id){
        return notesMapper.deleteByPrimaryKey(id);
    }

}
